<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片布局区域 -->
    <el-card class="box-card">
      <el-row :gutter="20">
        <!-- span 属性指的是占据多少列 -->
        <el-col :span="8">
          <!-- 搜索区域 -->
          <el-input v-model="queryInfo.query" placeholder="请输入内容" class="input-with-select" clearable @clear="getGoodsList">
            <el-button @click="getGoodsList" slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" plain @click="goAddGoods">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 商品列表表格 -->
      <el-table :data="goodsList" style="width: 100%" border stripe>
        <el-table-column align="center" type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="400"></el-table-column>
        <el-table-column align="center" prop="goods_price" label="商品价格（元）"></el-table-column>
        <el-table-column align="center" prop="goods_weight" label="商品重量"></el-table-column>
        <el-table-column align="center" prop="add_time" label="创建时间"></el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button plain size="mini" type="primary" icon="el-icon-edit"></el-button>
            <el-button @click="removeGoods(scope.row.goods_id)" plain size="mini" type="danger" icon="el-icon-delete"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'GoodsList',
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1, // 当前页码
        pagesize: 10 // 每页显示的条数
      },
      // 商品列表数据集合
      goodsList: [],
      // 所有的数据条数
      total: 0
    }
  },
  created() {
    // 调用获取商品列表数据的方法
    this.getGoodsList()
  },
  methods: {
    // 获取商品列表数据
    async getGoodsList() {
      const { data: res } = await this.$http.get('/goods', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)

      res.data.goods.forEach(item => {
        item.add_time = this.$dayjs(item.add_time).format('YYYY-MM-DD hh:mm')
      })

      this.goodsList = res.data.goods
      this.total = res.data.total
    },

    // 监听每页显示的条数，pageSize 改变时会触发
    handleSizeChange(pageSize) {
      this.queryInfo.pagesize = pageSize
      this.getGoodsList()
    },
    // 监听页码值有没有发生变化，当页面改变时会触发
    handleCurrentChange(pageNum) {
      this.queryInfo.pagenum = pageNum
      this.getGoodsList()
    },

    // 删除商品
    async removeGoods(id) {
      const confirmResult = await this.$confirm('此操作将永久删除该信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)

      if (confirmResult !== 'confirm') {
        return this.$message.info('您取消了删除')
      }

      const { data: res } = await this.$http.delete(`/goods/${id}`)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.$message.error(res.meta.msg)
      this.getGoodsList()
    },

    // 点击添加商品按钮，进入添加商品页面
    goAddGoods() {
      this.$router.push('/goods/addgoods')
    }
  }
}
</script>

<style lang="less" scoped></style>
